package cn.jsj.hzu.qm.facecloud.ciuser.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import cn.jsj.hzu.qm.facecloud.ciuser.dao.CilabDao;
import cn.jsj.hzu.qm.facecloud.ciuser.pojo.Cilab;

public class CilabDaoImpl implements CilabDao {

	JdbcTemplate jdbcTemplate = null;
	
	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	
	//增
	@Override
	public int addCilab(Cilab cilab) {
		String sql = "INSERT INTO tb_cilab VALUES(null, ?, ?, ?, 1)";
		int i = jdbcTemplate.update(sql, new Object[] {cilab.getName(), cilab.getLocation(), cilab.getMemo()});

		return i;
	}

	
	//改
	@Override
	public int modifyLab(Cilab cilab) {
		String sql = "UPDATE tb_cilab SET `name`=?, location=?, memo=?, available=? WHERE id=?";
		
		int i = jdbcTemplate.update(sql, new Object[] {cilab.getName(), cilab.getLocation(), cilab.getMemo(), cilab.getAvailable(), cilab.getId()});
		
		return i;
	}

	
	//查
	public Cilab findCilabById(int id)  {
		String sql = "SELECT * FROM tb_cilab WHERE id=?";
		try {
			Cilab cilab = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Cilab.class), new Object[] {id});
			return cilab;
		} catch (Exception e) {
			System.out.println("没有查询到实验室id！");
			return null;
		}
		
	}
	
	@Override
	public List<Cilab> getAllCilabs() {
		String sql = "SELECT * FROM tb_cilab";
		
		List<Cilab> cilabs = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Cilab.class));
		
		return cilabs;
	}
	
	
	@Override
	public List<String> getAllCilabName() {
		String sql = "SELECT `name` FROM tb_cilab";
		
		List<String> cilabNameStrings = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(String.class));
		
		return cilabNameStrings;
	}

	@Override
	public Cilab findCilabByName(String name) {
		String sql = "SELECT * FROM tb_cilab WHERE name=?";
//		System.out.println(name);
		try {
			Cilab cilab = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Cilab.class), new Object[] {name});
			return cilab;
		} catch (Exception e) {
//			e.printStackTrace();
			System.out.println("没有查询到实验室: "+name+" !");
			return null;
		}
	}

}
